<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CORS</title>
    <style>
      #result {
        width: 300px;
        height: 100px;
        border: 3px solid pink;
      }
    </style>
  </head>
  <body>
    <button>发送请求</button>
    <div id="result"></div>
    <script>
      const btn = document.querySelector("button");
      btn.onclick = function () {
        // 1.创建对象
        const xhr = new XMLHttpRequest();
        // 2.初始化设置
        xhr.open("GET", "http://127.0.0.1:8000/cors");
        // 3.发送请求
        xhr.send();
        // 4.绑定事件
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
              console.log(xhr.response);
              document.getElementById("result").innerText = xhr.response;
            }
          }
        };
      };
    </script>
  </body>
</html>
